/*
This program generates the decompsotion results using traditional 3-term: 
(1). scale
(2). technique
(3). reallocation
The emission intensity is constructed by using (1). emission/revenue and (2). emission/cost, respectively.

****




Notice that this file uses the formular in the appendix to compute every term
the file "2-term_decomposition(table4)" use the formular in the text compute the share


*/






****************************************************************************************
*---------------------------------------------------------------------------------------------
* 1. This part merge wage data to the multiple_decomposition (run first time)
/*
use "D:\Nanjing\2019\decomposition_data_file\code for submission\multiple_decomposition",clear
rename frdm party_id
capture drop _merge
merge m:m party_id year using "D:\Nanjing\2019\pollution2\data and do\wage_payable.dta"

keep if _merge==3
destring wage, replace force
gen cost=totalintermediate+wage
save "D:\Nanjing\2019\decomposition_data_file\code for submission\3_term_decomposition", replace

*------------------------------------------------------------------------------------------------

*use "D:\Nanjing\2019\decomposition_data_file\code for submission\3_term_decomposition",clear
gen MC_fp=expenditure_m_fjt_x/(theta2_fjt_x*quantity) /*MC_fp: firm-product level marginal cost*/

* 2. emission tax
replace so2_emission=exp(e) /*e contains predicted emission when emission==0*/
bysort year fid: egen ems_new=sum(c_x_new*so2_emission/theta2_E_fjt_x)
gen tax_emi=totaloutput/(Markup_firm*ems_new)  /*firm-year level emission tax*/

* 3. firm-level elasticity of emission
bysort year fid: egen theta2_E_fjt_firm=sum(theta2_E_fjt_x*c_x_new) /*theta2_E_fjt_firm: firm level elasticity of emission*/




*gen tau=1/tax_emi /*the measure of emission policy*/

egen ub_e=pctile(theta2_E_fjt_firm),p(97)
egen lb_e=pctile(theta2_E_fjt_firm),p(3)
*keep if theta2_E_fjt_firm>lb_e|theta2_E_fjt_firm<ub_e
gen indc_y=.
replace indc_y=1 if year==2000
replace indc_y=2 if year==2005

keep if indc_y==1|indc_y==2
*1. generate the number of products each firm produce in each year
gen indc_prod=1
bysort fid indc_y: egen num_prod=sum(indc_prod) /*num_product number of product at firm-year level*/



*****************************************************************
*--------------------------------------------------------------
* 2. Define 1. continuing, 2. new entrants and 3. exit firms.
* Only keep contiting firms for this decomposition
*---------------------------------------------------------------
capture drop party_id
replace year=indc_y
gen party_id=fid

gen dummy1=.
replace dummy1=1 if year==1&(totaloutput!=.&totaloutput!=0)

gen dummy2=.
replace dummy2=1 if year==2&(totaloutput!=.&totaloutput!=0)

sort party_id
by party_id: egen max_d1=max(dummy1)
by party_id: egen max_d2=max(dummy2)

replace dummy1=max_d1 if dummy1==.
replace dummy2=max_d2 if dummy2==.

* (1)status=1: continuing firms
* (2)status=2: exit
* (3)status=3: entrant
gen status=.
replace status=1 if dummy1==1&dummy2==1
replace status=2 if dummy1==1&dummy2==.
replace status=3 if dummy1==.&dummy2==1

bysort party_id: gen n1=_n==1 if status==1
bysort party_id: gen n2=_n==1 if status==2
bysort party_id: gen n3=_n==1 if status==3

* keep continuing firms
keep if status==1


sort party_id year quantity
duplicates drop party_id year, force  /*keep firm-year observations*/
save "D:\Nanjing\2019\decomposition_final\data\3_term_decomposition",replace
*/

use "D:\Nanjing\2019\decomposition_final\data\3_term_decomposition",clear
bysort year: egen total_ems=sum(so2_emission)


gen ems_rev=so2_emission/totaloutput
gen ems_cost=so2_emission/cost


bysort year: egen total_rev=sum(totaloutput)
bysort year: egen total_cost=sum(cost)
gen share_rev=totaloutput/total_rev
gen share_cost=cost/total_cost



egen C_2=max(total_cost)
egen C_1=min(total_cost)
egen R_2=max(total_rev)
egen R_1=min(total_rev)

gen delta_R=(R_2- R_1)/2
gen delta_C=(C_2- C_1)/2



tsset party_id year
sort party_id year

*keep if party_id==9
by party_id: gen share_rev_l=l.share_rev
by party_id: gen share_cost_l=l.share_cost

by party_id: gen ems_rev_l=l.ems_rev
by party_id: gen ems_cost_l=l.ems_cost
by party_id: gen cost_l=l.cost
by party_id: gen Markup_firm_l=l.Markup_firm




gen diff_share_rev=share_rev-share_rev_l
gen diff_share_cost=share_cost-share_cost_l

gen diff_ems_rev=ems_rev-ems_rev_l
gen diff_ems_cost=ems_cost-ems_cost_l
gen diff_markup=Markup_firm-Markup_firm_l
gen diff_cost=cost-cost_l

bysort party_id: egen Markup_l_new=mode(Markup_firm_l)
bysort year: egen m_markup=sum(cost*Markup_firm/total_cost)
bysort year: egen m_markup_l=sum(cost_l*Markup_firm_l/431300640) /*\mu_{t-1}*/
bysort party_id: egen share_cost_l_new=mode(share_cost_l)


*-----------------------------------------------------------------
*4.1 scale effect
egen aa=pctile(ems_cost),p(99.1)
bysort year: egen st_r=sum(share_rev*ems_rev) 
bysort year: egen st_c=sum(share_cost*ems_cost)  if ems_cost<aa

gen St_r=st_r*delta_R/*scale effect in revenue*/
gen St_c=st_c*delta_C *.9946772  /*scale effect in cost*/

replace St_r=. if year==1
replace St_c=. if year==1

*-------------------------------------------------------------------
* 4.2 scale bias

* (1). the scale measurement bias
bysort year: gen scale_b1=(delta_R-delta_C)
replace scale_b1=scale_b1*st_r

replace scale_b1=. if year==1

* (2). aggregation bias
bysort year: gen scale_b2=delta_C*(st_r-st_c)
replace scale_b2=. if year==1

sum St_r St_c 

*gen bias=scale_b1+scale_b2
gen bias=St_r-St_c
gen total_bias= scale_b1+scale_b2
*gen bias_s=(St_r-St_c)/total_bias /*the ratio of scale bais in total bias*/
*gen r_b1=scale_b1/bias*bias_s
*gen r_b2=scale_b2/bias*bias_s
gen r_b1=scale_b1/total_bias
gen r_b2=scale_b2/total_bias
sum bias r_b1 r_b2
sum scale_b1 scale_b2
gen r_b1_scale=r_b1*100
gen r_b2_scale=r_b2*100
drop bias r_b1 r_b2

***********************************************************************************
/*
* ------------------------------------------
* 5. Decomposition (reallocatiom)
Rt_r: reallocation effect using revenue
Rt_c: reallocation effect using cost
-------------------------------------------------
*/

*5.1 Reallocation effect
gen aaa1=diff_share_rev*ems_rev
gen aaa2=diff_share_cost*ems_cost
*egen ub_2=pctile(ems_rev_l),p(95)
egen lb_21=pctile(aaa1),p(1.5)
egen lb_22=pctile(aaa2),p(1.5)
bysort year: egen rt_r=sum(diff_share_rev*ems_rev_l) if aaa1>lb_21&aaa2>lb_22
bysort year: egen rt_c=sum(diff_share_cost*ems_cost_l) if aaa1>lb_21&aaa2>lb_22

gen Rt_r=rt_r*R_1/*multiply by aggregate revenue in last period R_(t-1)*/
gen Rt_c=rt_c*C_1*.9946772  /*multiply by aggregate revenue in last period R_(t-1)*/

replace Rt_r=. if year==1
replace Rt_c=. if year==1


*---------------------------------------------------------------------
* 5.2 Reallocation bais

*(1) reallocation measurement bias
bysort year: egen rt_b1=sum(diff_share_rev*ems_rev-diff_share_cost*ems_rev) 
replace rt_b1=rt_b1* R_1
replace rt_b1=. if year==1

bysort year: egen rt_b21=sum(diff_share_cost*ems_rev_l)
replace rt_b21=rt_b21*R_1

bysort year: egen rt_b22=sum(diff_share_cost*ems_cost_l) 
replace rt_b22=rt_b22*C_1

gen rt_b2=rt_b21-rt_b22
replace rt_b2=. if year==1


gen bias=rt_b1+rt_b2
gen bias_r=(Rt_r-Rt_c)/total_bias
gen r_b1=rt_b1/bias
gen r_b2=rt_b2/bias
sum bias r_b1 r_b2
gen r_b1_comp=r_b1*100
gen r_b2_comp=r_b2*100
drop bias r_b1 r_b2













***********************************************************************************
/*
* ------------------------------------------
* 6. Decomposition (Intensity)
it_r: intensity effect using revenue
it_c: intensity effect using cost
-------------------------------------------------
*/

* 6.1 intensity effect
gen bbb1=share_rev_l*diff_ems_rev
gen bbb2=share_cost_l*diff_ems_cost
capture drop lb_3
egen lb_31=pctile(bbb1), p(0.2)
egen lb_32=pctile(bbb2), p(0.4)

bysort year: egen it_r=sum(share_rev_l*diff_ems_rev)   
bysort year: egen it_c=sum(share_cost_l*diff_ems_cost)  if bbb1>lb_31&bbb2>lb_32

gen It_r=R_1*it_r
gen It_c=C_1*it_c*.9946772  


replace It_r=. if year==1
replace It_c=. if year==1





*----------------------------------------------------------------
*Notice: this fomular is according to the fumular in the appendix
*-------------------------------------------------------------------
bysort year: egen it_b1=sum(share_rev_l*(diff_ems_rev-diff_ems_cost))
replace  it_b1=it_b1*R_1




bysort year: egen it_b21=sum(share_rev_l*diff_ems_cost)


bysort year: egen it_b22=sum(share_cost_l*diff_ems_cost)

replace it_b21=it_b21*R_1
replace it_b22=it_b22*C_1

gen it_b2=it_b21-it_b22

replace it_b1=. if year==1
replace it_b2=. if year==1


format It_r It_c it_b1 it_b2
sum It_r It_c it_b1 it_b2

gen bias=it_b1+it_b2
gen bias_i=(It_r-It_c)/total_bias /*the ratio of intensity bias in total bias*/
gen r_b1=it_b1/bias
gen r_b2=it_b2/bias


sum bias r_b1 r_b2
gen r_b1_tech=r_b1*100
gen r_b2_tech=r_b2*100
drop bias r_b1 r_b2





* share of each component
gen Delta_r=St_r+Rt_r+It_r
gen Delta_c=St_c+Rt_c+It_c
gen diff=Delta_r/Delta_c
gen scale_share_r=St_r/Delta_r*100
gen scale_share_c=St_c/Delta_c*100

gen reallocation_share_r=Rt_r/Delta_r*100
gen reallocation_share_c=Rt_c/Delta_c*100


gen intensity_share_r=It_r/Delta_r*100
gen intensity_share_c=It_c/Delta_c*100


*---------------------------------------------------------------------------------
*  The upper panel of Table 4
*------------------------------------------------------------------------------------
sum scale_share_r reallocation_share_r intensity_share_r
sum scale_share_c reallocation_share_c intensity_share_c


*---------------------------------------------------------------------------------
*  The lower panel of Table 4
*------------------------------------------------------------------------------------

sum r_b1_scale r_b2_scale r_b1_comp r_b2_com r_b1_tech r_b2_tech


